﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Police_Question_QuestionNew : System.Web.UI.Page
{
    private string ConstDepartmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConstDepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!IsPostBack)
        {
            if (Request["ID"] != null)
            {
                Bind();
                
            }
            BindDroptDown();
        }
    }
    private void Bind()
    {
        Question2DataContext db = new Question2DataContext();
        var newobj = (from o in db.SecuritySubject2 where o.Id == new Guid(Request["ID"]) select o).Single();       
        newobj.CreateTime = DateTime.Now;
        txtCaption.Text=newobj.Caption;
        txtRemark.Text=newobj.Remak ;
        BtnAdd.Text = "修 改";
       
    }
    private void BindDroptDown()
    {
        Question2DataContext db = new Question2DataContext();
        var items = from i in db.SecuritySubject2 where i.CreateDepartment == ConstDepartmentID && !(from o in db.Examination2 select o.SID).Contains(i.Id) orderby i.CreateTime ascending select i;
        foreach(SecuritySubject2 item in items )
        {
            ListItem litem=new ListItem();
            int len=int.Parse(item.Flag.ToString());
            litem.Text="".PadRight(2 * len,'　')+item.Caption;
            litem.Value=item.Id.ToString();
            dptSubject.Items.Add(litem);
        }
        dptSubject.Items.Insert(0, new ListItem("顶级分类", "00"));
        
    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        if (txtCaption.Text.Trim() == "" )
        {
            lblPrompt.Text = "请填写完整";
        }
         
        try
        {
            
        Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        

        Question2DataContext db = new Question2DataContext();

        if (Request["ID"] != null)
        {
            var newobj = (from o in db.SecuritySubject2 where o.Id == new Guid(Request["ID"]) select o).Single();
            if (newobj == null)
            {
                lblPrompt.Text = "更新失败";
                return;
            }


            newobj.OverDate = DateTime.Now;
            newobj.CreateUser = user.UserID;
            newobj.CreateDepartment = user.DepartmentID;
            newobj.CreateTime = DateTime.Now;
            newobj.Caption = txtCaption.Text.Trim();
            newobj.Remak = txtRemark.Text.Trim();
            newobj.GuardCount = 60;
            db.SubmitChanges();
            lblPrompt.Text = "数据已更新";
            BtnAdd.Text = "修改";
            this.BtnAdd.Enabled = false;
        }
        else
        {

            SecuritySubject2 newobj = new SecuritySubject2();
            newobj.Id = Guid.NewGuid();
            newobj.OverDate = DateTime.Now;
            newobj.CreateUser = user.UserID;
            newobj.CreateDepartment = user.DepartmentID;
            newobj.CreateTime = DateTime.Now;
            newobj.Caption = txtCaption.Text.Trim();
            newobj.Remak = txtRemark.Text.Trim();
            newobj.GuardCount = 60;
            if (!dptSubject.SelectedValue.Equals("00"))
            {
                var obj = (from d in db.SecuritySubject2 where d.Id == new Guid(dptSubject.SelectedValue) select d).SingleOrDefault();
                int leven = int.Parse(((SecuritySubject2)obj).Flag.ToString());
                newobj.ParentID = new Guid(dptSubject.SelectedValue);
                newobj.Flag = leven + 1;
                newobj.HasChild=true;
            }
            else
            {
                newobj.Flag = 0;
            }

            db.SecuritySubject2.InsertOnSubmit(newobj);
            db.SubmitChanges();
            lblPrompt.Text = "添加成功";
            dptSubject.Items.Clear();
            BindDroptDown();
       
        }
        }
            catch( Exception ex){
                lblPrompt.Text = ex.Message.ToString();
            };

        
    }
    protected void btnParent_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuestionList.aspx");
    }
}
